package com.controller.gzp;

import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.stp.StpUtil;
import com.entity.Userinfo;
import com.hb.Insert;
import com.service.gzp.UserinfoService;
import com.util.gzp.AjaxResponse;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

/**
 * ClassName: LoginController
 * Package: com.controller.gzp
 *
 * @author: guanzepeng
 * @Date: 2021/4/6 13:57 星期二
 * @Description: 处理登录,注册,修改密码的接口
 */
@RestController
@Api (tags = "处理登录,注册,修改密码的接口")
@RequestMapping("/login")
@Validated
@Slf4j
public class LoginController {

	@Autowired
	private UserinfoService userinfoServiceImpl;

	/**
	 * 登录验证
	 *
	 * @param uname
	 * @param upass
	 * @return
	 */
	@PostMapping("/getlogin")
	@ApiOperation(value = "登录验证", response = AjaxResponse.class)
	@ApiImplicitParams({
			@ApiImplicitParam(name = "uname", value = "传入用户名", required = true, dataTypeClass = String.class),
			@ApiImplicitParam(name = "upass", value = "传入密码", required = true, dataTypeClass = String.class)
	})
	public AjaxResponse getUserLogin(@NotBlank(message = "用户名不能为空") String uname, @NotBlank(message = "密码不能为空") String upass) {
		return userinfoServiceImpl.getUserLogin(uname, upass);
	}

	/**
	 * 查询所有的用户,带分页,以及姓名的模糊查询
	 *
	 * @param uname    用户姓名
	 * @param pageNum  分页
	 * @param pageSize 分页
	 * @return 返回数据
	 */
	@GetMapping("/getUserList")
	@ApiOperation(value = "查询所有的用户,带分页,以及姓名的模糊查询", response = AjaxResponse.class)
	@ApiImplicitParams({
			@ApiImplicitParam(name = "uname", value = "传入用户姓名,可模糊", required = false, dataTypeClass = String.class),
			@ApiImplicitParam(name = "pageNum", value = "传入从第几条数据开始分页", required = false, defaultValue = "0", dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "pageSize", value = "传入每页显示几条数据", required = false, defaultValue = "3", dataTypeClass = Integer.class)
	})
	public AjaxResponse getUserList(String uname,
	                                @RequestParam(defaultValue = "0") Integer pageNum,
	                                @RequestParam(defaultValue = "3") Integer pageSize){
		return userinfoServiceImpl.getUserList(uname, pageNum, pageSize);
	}

	/**
	 * 软删除用户数据
	 *
	 * @param flag 传入true为删除,false为保留
	 * @param id   传入用户ID辨识用户
	 * @return 返回成功失败
	 */
	@DeleteMapping("/deleteUser")
	@ApiOperation(value = "软删除用户数据", response = AjaxResponse.class)
	@ApiImplicitParams({
			@ApiImplicitParam(name = "flag", value = "传入true为删除,false为保留", required = true, dataTypeClass = boolean.class),
			@ApiImplicitParam(name = "id", value = "传入用户ID辨识用户", required = true, dataTypeClass = Integer.class)
	})
	public AjaxResponse deleteUser(@NotNull(message = "布尔值不能为空") boolean flag, @NotNull(message = "用户ID不能为空") Integer id) {
		log.info("方法deleteUser传入的布尔值为:" + flag);
		return userinfoServiceImpl.deleteUser(flag, id);
	}

	/**
	 * 修改用户密码
	 *
	 * @param pwd    传入密码
	 * @param newPwd 传入新密码
	 * @return 返回成功失败
	 */
	@PostMapping("/updatePwd")
	@ApiOperation(value = "修改用户密码", response = AjaxResponse.class)
	@ApiImplicitParams({
			@ApiImplicitParam(name = "pwd", value = "传入旧密码", required = true, dataTypeClass = String.class),
			@ApiImplicitParam(name = "newPwd", value = "传入新密码", required = true, dataTypeClass = String.class)
	})
	public AjaxResponse updatePwd(@NotBlank(message = "旧密码不能为空") String pwd, @NotBlank(message = "新密码不能为空") String newPwd) {
		Integer uid = null;
		try {
			uid = StpUtil.getLoginIdAsInt();
		} catch (NotLoginException e) {
			return AjaxResponse.success("请先进行登录!", 500);
		}
		return userinfoServiceImpl.updatePwd(uid, pwd, newPwd);
	}

	/**
	 * 添加用户以及给用户授予权限
	 *
	 * @param userinfo 传入用户账号密码
	 * @param rid      传入权限ID
	 * @return 返回成功失败
	 */
	@PostMapping("/insertUser")
	@ApiOperation(value = "添加用户以及给用户授予权限", response = AjaxResponse.class)
	@ApiImplicitParam(name = "rid", value = "传入权限ID", required = true, dataTypeClass = Integer.class)
	public AjaxResponse insertUser(@Validated(Insert.class) Userinfo userinfo, @NotNull(message = "权限ID不能为空") Integer rid) {
		AjaxResponse ajaxResponse = userinfoServiceImpl.insertUser(userinfo, rid);
		return ajaxResponse;
	}

	/**
	 * 退出登录
	 *
	 * @return 返回成功失败
	 */
	@PostMapping("/checkOut")
	@ApiOperation(value = "退出登录", response = AjaxResponse.class)
	public AjaxResponse checkOut() {
		AjaxResponse ajaxResponse = userinfoServiceImpl.checkOut();
		return ajaxResponse;
	}

	/**
	 * 查询用户名
	 * @return 返回用户名
	 */
	@PostMapping("/getName")
	@ApiOperation(value = "查询用户名", response = AjaxResponse.class)
	public AjaxResponse getName() {
		AjaxResponse name = userinfoServiceImpl.getName();
		return name;
	}

}
